/**
 * \license
 * Copyright Lassi Marttala 2013
 * 
 * This file is part of dlt2jpa.
 *
 * dlt2jpa is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dlt2jpa is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with dlt2jpa.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.fizzl.dlt.message;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class DltPayload {
	@Id
	private long id;
	
	private short messageId;
	private byte[] dynamicData;
	
	public byte[] getDynamicData() {
		return dynamicData;
	}

	public void setDynamicData(byte[] dynamicData) {
		this.dynamicData = dynamicData;
	}

	@OneToMany(cascade = CascadeType.ALL)
	private List<DltPayloadArgument> arguments;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public short getMessageId() {
		return messageId;
	}

	public void setMessageId(short messageId) {
		this.messageId = messageId;
	}

	public List<DltPayloadArgument> getArguments() {
		return arguments;
	}

	public void setArguments(List<DltPayloadArgument> arguments) {
		this.arguments = arguments;
	}

	public int getLength() {
		int len = 0;
		if(messageId == 0) {
			len += 4;
			len += dynamicData.length;
		}
		else {
			for(DltPayloadArgument arg : arguments) {
				len += arg.getLength();
			}
		}
		return len;
	}

	public boolean addArgument(DltPayloadArgument e) {
		return arguments.add(e);
	}

	public void clearArguments() {
		arguments.clear();
	}

	public DltPayloadArgument getArgument(int index) {
		return arguments.get(index);
	}
}
